package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjTicketno;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.ISjTicketnoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 取号功能Controller
 * 
 * @author ruoyi
 * @date 2025-09-13
 */
@Controller
@RequestMapping("/gcgl/ticketno")
public class SjTicketnoController extends BaseController
{
    private String prefix = "sj/ticketno";

    @Autowired
    private ISjTicketnoService sjTicketnoService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IGcProjectService gcProjectService;

    @RequiresPermissions("gcgl:ticketno:view")
    @GetMapping()
    public String ticketno()
    {
        return prefix + "/ticketno";
    }

    /**
     * 查询取号功能列表
     */
    @RequiresPermissions("gcgl:ticketno:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjTicketno sjTicketno)
    {
        startPage();
        sjTicketno.setDeleteFlag(1);
        sjTicketno.setOwnerId(getUserId());
        List<SjTicketno> list = sjTicketnoService.selectSjTicketnoList(sjTicketno);
        return getDataTable(list);
    }

    /**
     * 导出取号功能列表
     */
    @RequiresPermissions("gcgl:ticketno:export")
    @Log(title = "取号功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjTicketno sjTicketno)
    {
    	sjTicketno.setDeleteFlag(1);
        List<SjTicketno> list = sjTicketnoService.selectSjTicketnoList(sjTicketno);
        ExcelUtil<SjTicketno> util = new ExcelUtil<SjTicketno>(SjTicketno.class);
        return util.exportExcel(list, "取号功能数据");
    }

    /**
     * 新增取号功能
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	mmap.put("clis", gcCustomerlis);
    	
    	mmap.put("sqAt", DateUtils.dateTime(new Date()));
    	
    	GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	gcProject.setStatus(2);
    	List<GcProject> polis = gcProjectService.selectGcProjectList(gcProject);
    	mmap.put("polis", polis);
        return prefix + "/add";
    }

    /**
     * 新增保存取号功能
     */
    @RequiresPermissions("gcgl:ticketno:add")
    @Log(title = "取号功能", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjTicketno sjTicketno){
    	
    	if(!"JYHB".equals(sjTicketno.getLeixing())){
    		String ticketNo = returnTicket(sjTicketno);
        	sjTicketno.setOrderNo(ticketNo);
    	}
    	
    	sjTicketno.setDeleteFlag(1);
    	sjTicketno.setStatus(0);
    	sjTicketno.setCreateUser(getLoginName());
    	sjTicketno.setCreateAt(new Date());
    	sjTicketno.setUpdateUser(getLoginName());
    	sjTicketno.setUpdateAt(new Date());
        return toAjax(sjTicketnoService.insertSjTicketno(sjTicketno));
    }
    
    //返回编号
    private String returnTicket(SjTicketno sjTicketno){
    	String ticketNo = "";
    	String leixing = sjTicketno.getLeixing();//公司--AAD/HB
    	String proType = sjTicketno.getProType();//建筑，景观
    	Integer qhd = sjTicketno.getQhd();//设计一所--合作
    	
    	SjTicketno obj = new SjTicketno();
    	obj.setDeleteFlag(1);
    	obj.setLeixing(leixing);
    	List<SjTicketno> lisCount = sjTicketnoService.selectSjTicketnoList(obj);
    	
    	SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
		int year = Integer.parseInt(sdy.format(new Date()));
		String no = String.format("%03d", lisCount.size()+1);
    	
		if(leixing.equals("AAD/HB")){
			if(qhd==4){
	    		//合作	AAD/HB-2025-JZ001
	    		ticketNo = leixing+"-"+year+"-"+proType+no;		
	    	}else{
	    		//设计一所....三所	AAD/HB1-2025-001
	    		ticketNo = leixing+qhd+"-"+year+"-"+no;	
	    	}
		}else if(leixing.equals("JYHB")){
			//设计一所....三所\合作	JYHB-2025-001
    		ticketNo = leixing+"-"+year+"-"+no;	
		}else if(leixing.equals("ZF-HB")){
			SimpleDateFormat sdyzf = new SimpleDateFormat("yy");
			int yearzf = Integer.parseInt(sdyzf.format(new Date()));
			//设计一所....三所\合作	ZF-HB-25001
    		ticketNo = leixing+"-"+yearzf+no;	
		}
		return ticketNo;
    }

    /**
     * 修改取号功能
     */
    @RequiresPermissions("gcgl:ticketno:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjTicketno sjTicketno = sjTicketnoService.selectSjTicketnoById(id);
        mmap.put("sjTicketno", sjTicketno);
        
        GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	gcProject.setStatus(2);
    	List<GcProject> polis = gcProjectService.selectGcProjectList(gcProject);
    	mmap.put("polis", polis);
        return prefix + "/edit";
    }

    /**
     * 修改保存取号功能
     */
    @RequiresPermissions("gcgl:ticketno:edit")
    @Log(title = "取号功能", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjTicketno sjTicketno)
    {
    	if(!"JYHB".equals(sjTicketno.getLeixing())){
    		String ticketNo = returnTicket(sjTicketno);
        	sjTicketno.setOrderNo(ticketNo);
    	}
    	
    	sjTicketno.setUpdateUser(getLoginName());
    	sjTicketno.setUpdateAt(new Date());
        return toAjax(sjTicketnoService.updateSjTicketno(sjTicketno));
    }

    /**
     * 删除取号功能
     */
    @RequiresPermissions("gcgl:ticketno:remove")
    @Log(title = "取号功能", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjTicketnoService.deleteSjTicketnoByIds(ids));
    }
}